# Maintainer: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
# Contributor: Hannes Graeuler <hgraeule @ uos dot de>

# this is needed only for building the imagequant-sys crate
_libver=4.1.1

pkgname=pngquant
pkgver=3.0.1
pkgrel=3
pkgdesc="Command-line utility to quantize PNGs down to 8-bit paletted PNGs"
arch=('loong64' 'x86_64')
url="https://pngquant.org/"
license=('BSD')
depends=('libpng' 'lcms2' 'libimagequant')
makedepends=('cargo')
source=("https://github.com/kornelski/$pkgname/archive/$pkgver/$pkgname-$pkgver.tar.gz"
        "https://github.com/ImageOptim/libimagequant/archive/$_libver/libimagequant-$_libver.tar.gz")
sha256sums=('488e4587c27c7515427b231530cc070a6b123727137b32c501658cff3f8a3451'
            '726ce0bf2a8175a32d1ef303c3e8971a1fda354e760f1327c08656268bb019c2')

prepare() {
    cd "$srcdir/$pkgname-$pkgver"
    rm -rf lib
    ln -s ../libimagequant-$_libver lib
#    cargo fetch --target "$CARCH-unknown-linux-gnu"
}

build() {
    cd "$srcdir/$pkgname-$pkgver"
    export RUSTUP_TOOLCHAIN=stable
    export CARGO_TARGET_DIR=target
    find -name Cargo.lock -exec rm -f {} \;
    mkdir -p .cargo
    cat > .cargo/config.toml <<EOF
[source.crates-io]
registry = "https://gitee.com/yetist/crates.io-index"
EOF
    cargo build --release
}

check() {
    cd "$srcdir/$pkgname-$pkgver"
    export RUSTUP_TOOLCHAIN=stable
    cargo test
}

package() {
    cd "$srcdir/$pkgname-$pkgver"
    install -Dm755 target/release/pngquant "$pkgdir/usr/bin/pngquant"
    install -Dm644 pngquant.1 "$pkgdir/usr/share/man/man1/pngquant.1"
    install -Dm644 COPYRIGHT "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
